const fs = require('fs');
const path = require('path');
const ds = require('./tools/tool');
const tool = require('../tool');

async function getAllModules() {
    const modulesDir = path.join(__dirname, 'modules')
    let parsedModules = []
    fs.readdirSync(modulesDir).forEach(file => {
        if (path.extname(file) === '.js') {
            const modulePath = path.join(modulesDir, file)
            const module = require(modulePath)
            if (typeof module === 'object') {
                parsedModules.push({
                    file: file.replace('.js', ''),
                    o: {
                        ...module
                    }
                });
            }
        }
    });
    // await tool.wait(1)
    return parsedModules;
}

module.exports = async  () => {
    let modules = await getAllModules()
    let parsedPorts = []
    for (let i = 0; i < modules.length; i++) {
        let module = modules[i]
        const parseAllPorts = (o, currentName = '') => {
            ds.object.for(o, (k, v) => {
                if (v.func == undefined) {
                    parseAllPorts(v, currentName + '/' + k)
                } else {
                    let c = {
                        api: currentName + '/' + k,
                        ...v
                    }
                    parsedPorts.push(c)
                }
            })
        }
        parseAllPorts(module.o, `/${module.file}`)
    }
    await tool.wait(0.5)
    // console.log(parsedPorts)
    return parsedPorts
}